package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.DataDetail;
import cn.wolfcode.crm.domain.DataDictionary;
import cn.wolfcode.crm.mapper.DataDetailMapper;
import cn.wolfcode.crm.mapper.DataDictionaryMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDataDictionaryService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DataDictionaryServiceImpl implements IDataDictionaryService {

    @Autowired
    DataDictionaryMapper mapper;
    @Autowired
    DataDetailMapper dataDetailMapper;

    public void saveOrUpdate(DataDictionary record) {
        if (record.getId() == null) {
            mapper.insert(record);
        } else {
            mapper.updateByPrimaryKey(record);
        }
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
        List<DataDetail> dataDetails = dataDetailMapper.selectByData(id);
        for (DataDetail dataDetail : dataDetails) {
            dataDetailMapper.deleteByPrimaryKey(dataDetail.getId());
        }
    }

    public PageResult query(QueryObject qo) {
        int rows = mapper.queryForCount();
        if (rows == 0) {
            return new PageResult(rows, Collections.EMPTY_LIST);
        }
        List<?> result = mapper.queryForList(qo);
        return new PageResult(rows,result);
    }

    public List<DataDictionary> selectAll() {
        return mapper.selectAll();
    }
}
